package com.linkhub.oms.admin.modules.logistic.rules.domain;
import com.google.common.collect.Lists;

import cn.hutool.core.convert.Convert;
import com.linkhub.oms.admin.modules.logistic.domain.dto.ShippingChannelDTO;
import com.linkhub.oms.common.util.QtOptionalUtil;
import com.linkhub.oms.entity.LocalProductEntity;
import com.linkhub.oms.entity.OrderAddressEntity;
import com.linkhub.oms.entity.OrderEntity;
import com.linkhub.oms.entity.resulttype.order.OrderInfoDO;
import lombok.Data;
import org.apache.commons.lang3.StringUtils;

import java.io.Serializable;
import java.math.BigDecimal;
import java.util.List;
import java.util.Objects;

/**
 * @Description: ChannelRuleMatchRequest
 * @Author: echo
 * @Date: 2024/6/1 15:36
 * @Version: 1.0
 */
@Data
public class ChannelRuleMatchRequest implements Serializable {

    private ShippingChannelDTO channel;

    private OrderInfoDO orderInfoDO;

    private List<LocalProductEntity> localProducts;

    public static ChannelRuleMatchRequest createDefaultRequest(ShippingChannelDTO channel, OrderInfoDO orderInfoDO) {
        ChannelRuleMatchRequest request = new ChannelRuleMatchRequest();
        request.setChannel(channel);
        request.setOrderInfoDO(orderInfoDO);
        request.setLocalProducts(Lists.newArrayListWithCapacity(0));

        return request;
    }

    public String assemblyChannelCountryKey() {
        return String.format("%s_%s", acquireChannelCode(), acquireOrderCountryCode());
    }

    public String acquireChannelCode() {
        return QtOptionalUtil.of(channel)
                .map(ShippingChannelDTO::getChannelCode)
                .orElse(StringUtils.EMPTY);
    }

    public String acquireOrderCountryCode() {
        return QtOptionalUtil.of(orderInfoDO)
                .map(OrderInfoDO::getOrder)
                .map(OrderEntity::getOrderAddress)
                .map(OrderAddressEntity::getCountryCode)
                .orElse(StringUtils.EMPTY);
    }

    public String acquireOrderCurrency() {
        return QtOptionalUtil.of(orderInfoDO)
                .map(OrderInfoDO::getOrder)
                .map(OrderEntity::getCurrency)
                .orElse(StringUtils.EMPTY);
    }

    public BigDecimal acquireOrderTotalPrice() {
        return QtOptionalUtil.of(orderInfoDO)
                .map(OrderInfoDO::getOrder)
                .map(OrderEntity::getCurrentTotalPrice)
                .map(price -> Convert.toBigDecimal(price, BigDecimal.ZERO))
                .orElse(BigDecimal.ZERO);
    }

    public int acquireOrderProductQuantity() {

        return QtOptionalUtil.of(orderInfoDO)
                .map(OrderInfoDO::acquireOrderProductQuantity)
                .orElse(0);
    }


}
